<?php
class admin_Model_Weight extends Clu_Db_Table_Abstract
{
    //only specify the table name, everything else inherits
	protected $_name = "widget_item_weights";
	protected $_primary = "id";

	/* @param: $data is an array of 
	 *	widget_position_id, 'content_module', 'type' (optional), 'content_id', 'weight' (optional)
	*/
	public function insertRaw($data)
	{
		$db = $this->getAdapter();
		//widget_position_id, 'content_module', 'type', 'content_id', 'weight', 'content_title'
		$sql = "SELECT content_id FROM " . $this->_name . " WHERE 1 ";
		foreach ($data as $key => $val)
		{
			if ($key == "content_id")
				$sql = $sql . " AND content_id=". $db->quote($val, 'INTEGER') ;
			else
				$sql = $sql . " AND $key=". $db->quote($val, 'STRING') ;
		}			 
		$ret = $db->fetchAll($sql);
		//row already exists
		if($ret)
	       	return false;		
		else
		{
			try 
			{ 
				$ret = $this->insert($data);
			}
			catch (Exception $e)
			{
				die_a($e->getMessage());
			}
			
		}
		return $ret;
	}

	/* @param: $data is an array of 
	 *	'widget_position_id', 'content_module', 'type' (optional), 'content_id', 'weight' (optional)
	*/
	
	public function deleteRaw($data)
	{
		$db = $this->getAdapter();
		$sql = "DELETE FROM " . $this->_name ." WHERE 1 ";
		$sql = $sql . " AND widget_position_id=". $db->quote($data['widget_position_id'], 'INTEGER') ;
		$sql = $sql . " AND content_id=". $db->quote($data['content_id'], 'INTEGER') ;			
		try 
		{		 
       		$db->query($sql);
       		return true;
		}
		catch (Exception $e)
		{
			return false;
		}
	}


	/* @param: $data is an array of 
	 *	'widget_position_id', 'content_module', 'type' (optional), 'content_id', 'weight' (optional)
	 * Join `widget_item_weights` and content table, (places, events, nodes...) to get the name
	 * from the content table
	 */
	
	public function search($data)
	{
		if(isset($data))
		{
			if($data['content_module'] == 'ads')
				$sql = "SELECT * FROM " . $this->_name . " INNER JOIN " .
					 $data['content_module'] . " ON " . $data['content_module'].".id = " . 
					 $this->_name . ".content_id WHERE 1";
			else
				$sql = "SELECT * FROM " . $this->_name . " INNER JOIN " .
				 $data['content_module'] . "s ON " . $data['content_module']."s.id = " . 
				 $this->_name . ".content_id WHERE 1";
				
			foreach ($data as $key => $val)
			{
				if ($key == "content_id")
					$sql = $sql . " AND content_id=". $this->getAdapter()->quote($val, 'INTEGER') ;
				else 
				{
					if($key == "type")
						$sql = $sql . " AND " . $this->_name. '.' . "$key=" . $this->getAdapter()->quote($val, 'STRING');
					else
						$sql = $sql . " AND $key=". $this->getAdapter()->quote($val, 'STRING') ;
				}
			}
			$sql = $sql . " ORDER BY " .$this->_name . ".weight LIMIT 20";
		}
		else
			$sql = "SELECT * FROM " . $this->_name . " WHERE 1";
//		die($sql);
       	return $this->getAdapter()->fetchAll($sql);
	}
	
	public function searchCurrentItemsByWidgetPositionId($data)
	{
		$sql =  "SELECT * FROM " . $this->_name . " WHERE widget_position_id=" . $data['widget_position_id'] . 
		" ORDER BY weight LIMIT 20";
       	return $this->getAdapter()->fetchAll($sql);
	}
	
	public function updatedRaw($data)
	{
		foreach($data as $k => $v)
		{
			$sql = "UPDATE " . $this->_name ." SET weight = ".$k." WHERE content_id = " . $v;
			$this->getAdapter()->query($sql);
		}
       	return true;
	}
	
	public function getDataWidgetPosition($data)
	{
		$contentModule = $data['content_module'];
		$site = $data['site'];
		$theme = $data['themes'];
		$sql = "SELECT DISTINCT master_request
						FROM widget_positions 
						WHERE site ='$site' 
						 AND theme='$theme'
						 AND master_request like '$contentModule%' ";//, $site, $theme);
		return $this->getAdapter()->fetchAll($sql);	   
	}
	
	public function getWidgets($masterRequest)
	{
		$sql = "SELECT DISTINCT * FROM widget_positions WHERE master_request = '$masterRequest'";
		return $this->getAdapter()->fetchAll($sql);	   
	}
	
	public function getWidgetInfo($widgetPositionId)
	{
		$sql = "SELECT * FROM widget_positions WHERE id=$widgetPositionId";
		return $this->getAdapter()->fetchAll($sql);	   
	}
	
}	

?>